import { useEffect, useState } from 'react'
import { getMineListApi, MineDel } from '@/apis/mine'
import { message } from 'antd'

//获取用户列表信息
function useMine() {
    //获取文章列表
    const [mineList, setMineList] = useState([])

    //获取文章列表参数
    const [reqData, setReqData] = useState({
        page: 1,
        per_page: 20,
        MaxResultCount: 20,
        SkipCount: 0
        // display_name_cn: null,
        // address: null,
        // path: null,
        // authentication_mode: null,
        // start_time: null,
        // end_time: null,
    })

    const [count, setCount] = useState(0)
    //获取筛选数据
    const onFinish = (formValue) => {
        // console.log(formValue)
        // 1. 准备参数
        const { display_name_cn, address, path, authentication_mode, ceate_time } = formValue
        setReqData({
            ...reqData,
            page: 1,
            per_page: 20, 
            MaxResultCount: 20,
            SkipCount: 0,
            // display_name_cn: display_name_cn,
            // address: address,
            // path: path,
            // authentication_mode: '',
            start_time: ceate_time == undefined ? null : ceate_time[0].format('YYYY-MM-DD'),
            end_time: ceate_time == undefined ? null : ceate_time[1].format('YYYY-MM-DD'),
        })
    }

    //获取异步数据
    const getList = async function getList() {
        const res = await getMineListApi(reqData)
        setCount(res.data.totalCount)
        setMineList(res.data.items)
    }

    //用户列表列表
    useEffect(() => {
        getList()
    }, [reqData])

    //列表分页
    const onPageChange = (val) => {
        // console.log(val)
        setReqData({
            ...reqData,
            page: val,
            MaxResultCount: 20,
            SkipCount: (val - 1) * 20
        })
    }

    const onConfirm = async (id) => {
        const res = await MineDel(id)
        if (res.code == 200) {
            message.success("删除成功")
            getList()
        }
        else {
            message.error(res.message)
        }
    }

    return { reqData, count, mineList, onConfirm, onPageChange, onFinish, getList }
}

export default useMine